/*
 ------------------------------------------------------------------------------
 Copyright (C) 2013 Eternal Games.

 This file is part of the GLQuake source code.

 The GLQuake source code is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at your
 option) any later version.

 The GLQuake source code is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 more details.

 You should have received a copy of the GNU General Public License along with
 the GLQuake source code; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ------------------------------------------------------------------------------
*/


//
// cmdSystem.h - framework used by dynamic-link libraries
//


#ifndef __FRAMEWORK_CMDSYSTEM_H__
#define __FRAMEWORK_CMDSYSTEM_H__


/*
 ==============================================================================

 Console command execution and command text buffering:

 Any number of commands can be added in a frame from several different sources.
 Most commands come from either key bindings or console line input, but entire
 text files can be executed.
 The + command line arguments are also added to the command buffer.

 Command execution takes a NULL terminated string, breaks it into tokens, then
 searches for a command or variable that matches the first token.

 ==============================================================================
*/

typedef void				(*cmdFunction_t)();
typedef void				(*argCompletion_t)(void (*callback)(const char *string));

enum cmdExec_t {
	CMD_EXEC_APPEND,							// Append at the end of the command buffer
	CMD_EXEC_INSERT,							// Insert at the beginning of the command buffer
	CMD_EXEC_NOW								// Don't return until completed
};

// Functions exported by the engine
struct glqCmdSystem_t {
	// Adds a command and the function to call for it.
	// If function is NULL, the command will be forwarded to the server instead
	// of executed locally.
	// The description parameter can be NULL if you don't want it.
	// The argCompletion parameter can be NULL, one of the default argument
	// completion functions, or a custom argument completion function.
	void					(*AddCommand)(const char *name, cmdFunction_t function, const char *description, argCompletion_t argCompletion);

	// Removes a command
	void					(*RemoveCommand)(const char *name);

	// Base for path/file argument completion
	void					(*ArgCompletion_PathExtension)(void (*callback)(const char *string), const char *path, const char *extension, bool stripPath);

	// Base for definition argument completion
	void					(*ArgCompletion_Definition)(void (*callback)(const char *string), int type);

	// Default argument completion functions
	void					(*ArgCompletion_ConfigName)(void (*callback)(const char *string));
	void					(*ArgCompletion_DemoName)(void (*callback)(const char *string));
	void					(*ArgCompletion_MapName)(void (*callback)(const char *string));
	void					(*ArgCompletion_ModelName)(void (*callback)(const char *string));
	void					(*ArgCompletion_ImageName)(void (*callback)(const char *string));
	void					(*ArgCompletion_SoundName)(void (*callback)(const char *string));
	void					(*ArgCompletion_MusicName)(void (*callback)(const char *string));
	void					(*ArgCompletion_SpriteName)(void (*callback)(const char *string));
	void					(*ArgCompletion_SkinName)(void (*callback)(const char *string));
	void					(*ArgCompletion_MaterialName)(void (*callback)(const char *string));
	void					(*ArgCompletion_SoundShaderName)(void (*callback)(const char *string));
	void					(*ArgCompletion_ParticleName)(void (*callback)(const char *string));
	void					(*ArgCompletion_FXName)(void (*callback)(const char *string));
	void					(*ArgCompletion_GUIName)(void (*callback)(const char *string));

	// Returns the number of arguments
	int						(*Argc)();

	// Returns the given argument.
	// Will return an empty string, not NULL, if arg < 0 or arg >= argc, so
	// string operations are always safe.
	const char *			(*Argv)(int arg);

	// Returns a string containing argv[start] to argv[end]. Specifying 1 for
	// start and -1 for end will return all the arguments. Specifying 0 for
	// start will also include the command.
	// If tokenized is false, the original, unmodified command string will be
	// returned.
	const char *			(*Args)(int start, int end, bool tokenized);

	// Parses the given string into command line tokens. Does not need to be \n
	// terminated.
	// The text is copied to a separate buffer and 0 characters are inserted in the
	// appropriate place. The argv array will point into this temporary buffer.
	void					(*TokenizeString)(const char *text);

	// Appends command text at the end of the command buffer. Adds a final \n.
	void					(*AppendText)(const char *text);

	// Inserts command text at the beginning of the command buffer. Adds a
	// final \n.
	void					(*InsertText)(const char *text);

	// This can be used in place of either AppendText or InsertText
	void					(*ExecuteText)(cmdExec_t exec, const char *text);
};


#endif	// __FRAMEWORK_CMDSYSTEM_H__